package com.sky.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.constant.MessageConstant;
import com.sky.constant.StatusConstant;
import com.sky.dto.SetmealDTO;
import com.sky.dto.SetmealPageQueryDTO;
import com.sky.entity.Dish;
import com.sky.entity.Setmeal;
import com.sky.entity.SetmealDish;
import com.sky.exception.DeletionNotAllowedException;
import com.sky.exception.SetmealEnableFailedException;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealDishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.result.PageResult;
import com.sky.service.SetmealService;
import com.sky.vo.DishItemVO;
import com.sky.vo.SetmealVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.stereotype.Service;
import java.util.List;

@Service
@Slf4j
public class SetmealServiceImpl implements SetmealService {

    @Autowired
    private SetmealMapper setmealMapper;
    @Autowired
    private SetmealDishMapper setmealDishMapper;
    @Autowired
    private DishMapper dishMapper;

    /**
     * 套餐分页查询
     * @param setmealPageQueryDTO
     * @return
     */
    @Override
    public PageResult pageQuery(SetmealPageQueryDTO setmealPageQueryDTO) {

        //数据库底层代码 select * from employee limit 0,10     分页查询10条数据
        //开始分页
        PageHelper.startPage(setmealPageQueryDTO.getPage(),setmealPageQueryDTO.getPageSize());

        Page<SetmealVO> page = setmealMapper.pageQuery(setmealPageQueryDTO);

        long total = page.getTotal();
        List<SetmealVO> result = page.getResult();

        return new PageResult(total,result);
    }

    /**
     * 新增套餐
     * @param setmealDTO
     */
    @Override
    @CacheEvict(cacheNames = "setmealCache",allEntries = true)
    public void save(SetmealDTO setmealDTO) {
        Setmeal setmeal = new Setmeal();
        //拷贝setmealDTO-->setmeal
        BeanUtils.copyProperties(setmealDTO,setmeal);
        setmealMapper.save(setmeal);
        log.info("本次新增的菜品id为：{}",setmealDTO.getId());
        //获取自增生成的id
        Long setmealId = setmeal.getId();

        //新增套餐菜品关联关系表
        List<SetmealDish> setmealDishes = setmealDTO.getSetmealDishes();
        if(setmealDishes != null && setmealDishes.size() > 0){
            for (SetmealDish setmealDish : setmealDishes) {
                setmealDish.setSetmealId(setmealId);
                setmealDishMapper.save(setmealDish);
            }
        }


    }

    /**
     * 根据id查询套餐
     * @param id
     * @return
     */
    @Override
    public SetmealVO getById(Long id) {
        SetmealVO setmealVO = new SetmealVO();
        Setmeal setmeal = setmealMapper.getById(id);
        BeanUtils.copyProperties(setmeal,setmealVO);

        List<SetmealDish> setmealDishes = setmealDishMapper.getBySetmealId(id);

        setmealVO.setSetmealDishes(setmealDishes);

        return setmealVO;
    }

    /**
     * 修改套餐
     * @param setmealDTO
     */
    @Override
    @CacheEvict(cacheNames = "setmealCache",allEntries = true)
    public void updateSetmeal(SetmealDTO setmealDTO) {
        //修改套餐
        Setmeal setmeal = new Setmeal();
        //拷贝setmealDTO-->setmeal
        BeanUtils.copyProperties(setmealDTO,setmeal);
        setmealMapper.updateSetmeal(setmeal);

        //删除旧关联关系
        Long setmealId = setmeal.getId();
        setmealDishMapper.delBySetmealId(setmealId);
        //获取传入的套餐菜品信息
        List<SetmealDish> setmealDishes = setmealDTO.getSetmealDishes();
        //新增菜品
        if(setmealDishes != null && setmealDishes.size() > 0){
            for (SetmealDish setmealDish : setmealDishes) {
                setmealDish.setSetmealId(setmealId);
                setmealDishMapper.save(setmealDish);
            }
        }
    }

    /**
     * 停售起售
     * @param status
     * @param id
     */
    @Override
    @CacheEvict(cacheNames = "setmealCache",allEntries = true)
    public void status(Integer status, Long id) {

        //套餐内包含未启售菜品，无法启售
        List<SetmealDish> setmealDishes = setmealDishMapper.getBySetmealId(id);
        //遍历套餐菜品表获取菜品id
        for (SetmealDish setmealDish : setmealDishes) {
            //查询菜品，并判断菜品是否删除
            Dish dish = dishMapper.getById(setmealDish.getDishId());
            if(dish.getStatus() == StatusConstant.DISABLE){
                throw new SetmealEnableFailedException(MessageConstant.SETMEAL_ENABLE_FAILED);
            }
        }
        Setmeal setmeal = Setmeal.builder().status(status).id(id).build();
        setmealMapper.status(setmeal);
    }

    /**
     * 批量删除套餐
     * @param ids
     */
    @Override
    @CacheEvict(cacheNames = "setmealCache",allEntries = true)
    public void delSetmeal(List<Long> ids) {

        for (Long id : ids) {
            //在售套餐无法删除
            if(setmealMapper.getById(id).getStatus() == StatusConstant.ENABLE){
                throw new DeletionNotAllowedException(MessageConstant.SETMEAL_ON_SALE);
            }
            //删除套餐
            setmealMapper.delSetmeal(id);

            //删除套餐菜品关系表
            setmealDishMapper.delBySetmealId(id);
        }
    }

    /**
     * 根据分类id查询套餐
     * @param categoryId
     * @return
     */
    @Override
    public List<SetmealVO> list(Long categoryId) {
        List<SetmealVO> list = setmealMapper.list(categoryId);
        log.info("套餐为：{}",list);
        return list;
    }

    /**
     * 根据套餐id查询包含的菜品
     * @param setmealId
     * @return
     */
    @Override
    public List<DishItemVO> listDish(Long setmealId) {

        return setmealMapper.listDish(setmealId);
    }
}
